In [1]:
import requests
In [2]:
req=requests.get('http://httpbin.org/get')
In [3]:
print(req.text)
In [4]:
parameters={'user':'xianjunzheng','password':'1234'}
req=requests.post('http://httpbin.org/post',params=parameters)
In [5]:
print(req.text)
基本的登陆
In [6]:
url='http://httpbin.org'
In [7]:
req=requests.get(url+'/basic-auth/user/passwd',auth=('user','passwd'))
In [8]:
print(req.text)
In [9]:
print(req.url)
In [10]:
print(req.status_code)
In [11]:
import json
In [12]:
payload={'some':'data'}
headers={'Content-Type':'application/json','Authorization':'some token'}
req=requests.post(url+'/post',data=json.dumps(payload),headers=headers)
In [13]:
print(req.text)
In [14]:
req=requests.post(url+'/post',data=payload)
In [15]:
print(req.text)
In [16]:
files={'file':open('dump.txt','rb')}
In [17]:
req=requests.post(url+'/post',files=files)
In [18]:
print(req.text)
In [19]:
print(req.status_code)
In [20]:
req.status_code==requests.codes.ok
Out[20]:
In [21]:
print(req.headers)
In [22]:
req.headers['content-type']
Out[22]:
sending cookie
In [23]:
cookies={}
cookies['cookie']='cookie-value'
In [24]:
req=requests.get(url+'/cookies',cookies=cookies)
In [25]:
print(req.text)
Re-direction 302 means the URL has been redirected to some other location. We could use allow_redirects=False to disable this feature.
In [26]:
req=requests.head('http://www.google.com',allow_redirects=True)
In [27]:
print(req.url)
In [28]:
req.history
Out[28]:
In [29]:
req=requests.head('http://www.google.com',allow_redirects=False)
In [30]:
print(req.text)
time out
In [31]:
try:
req=requests.get('http://google.com',timeout=0.03)
except BaseException as e:
print("It is exceeding timeout")
print(str(e))
session session can persist cookies across requests
In [32]:
import requests
session=requests.Session()
In [33]:
session.headers.update(headers)
session.data='some data here'
session.params={'key1':'value1','key2':'value2'}
session.auth=('user','passwd')
In [34]:
req=session.get(url+'/basic-auth/user/passwd')
In [35]:
print(req.text)
In [36]:
req=session.get(url+'/get')
In [37]:
print(req.text)
In [38]:
req=session.post(url+'/post',data='some data here')
In [39]:
print(req.text)
session can be overriden
In [40]:
req=session.get(url+'/get',headers={'Content-Type':'application','User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'})
In [41]:
print(req.text)
这里的req是server返回的response 当requests.get()或者session.get(),首先发生的事情是构造了一个request,这个request将被发送个server,同时req里也将保存这个request 接下来req就接收server返回的response
In [47]:
print(req.request)
print(req.request.headers)
print(req.request.body)
In [49]:
print(req.headers)
print(req.text)
In [ ]: